<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>fuzzy 查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl-fuzzy-query.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-fuzzy-query.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-fuzzy-query.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl-fuzzy-query.html" rel="nofollow" target="_blank">../en/query-dsl-fuzzy-query.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="query-dsl.html">查询领域特定语言(Query DSL)</a></span>
»
<span class="breadcrumb-link"><a href="term-level-queries.html">词级查询</a></span>
»
<span class="breadcrumb-node">fuzzy 查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="query-dsl-exists-query.html">« exists 查询</a>
</span>
<span class="next">
<a href="query-dsl-ids-query.html">ids »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-fuzzy-query"></a>模糊查询 (fuzzy query)
</h2>
</div></div></div>

<p>
根据<a href="http://en.wikipedia.org/wiki/Levenshtein_distance" class="ulink" target="_top">莱温斯坦编辑距离</a>，返回包含与搜索词相似的词项的文档。
</p>
<p>
编辑距离是将一个词转换成另一个词所需的单个字符的变化次数。这些变化包括:
</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
改变一个字符 (<span class="strong strong"><strong>b</strong></span>ox → <span class="strong strong"><strong>f</strong></span>ox)
</li>
<li class="listitem">
删除一个字符 (<span class="strong strong"><strong>b</strong></span>lack → lack)
</li>
<li class="listitem">
插入一个字符 (sic → sic<span class="strong strong"><strong>k</strong></span>)
</li>
<li class="listitem">
调换两个相邻的字符 (<span class="strong strong"><strong>ac</strong></span>t → <span class="strong strong"><strong>ca</strong></span>t)
</li>
</ul>
</div>
<p>
为了找到相似的词，<code class="literal">fuzzy</code> 查询在指定的编辑距离内创建搜索词的所有可能变化或扩展的集合。

然后，该查询返回每个扩展的精确匹配。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="fuzzy-query-ex-request"></a>请求示例
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="fuzzy-query-ex-simple"></a>一个简单的例子
</h4>
</div></div></div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "query": {
        "fuzzy": {
            "user": {
                "value": "ki"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/277.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="fuzzy-query-ex-advanced"></a>一个使用高级参数的例子
</h4>
</div></div></div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "query": {
        "fuzzy": {
            "user": {
                "value": "ki",
                "fuzziness": "AUTO",
                "max_expansions": 50,
                "prefix_length": 0,
                "transpositions": true,
                "rewrite": "constant_score"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/278.console"></div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="fuzzy-query-top-level-params"></a><code class="literal">fuzzy</code>的顶级参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">&lt;field&gt;</code>
</span>
</dt>
<dd>
(必需, object) 你想搜索的字段。
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="fuzzy-query-field-params"></a><code class="literal">&lt;field&gt;</code>的参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">value</code>
</span>
</dt>
<dd>
(必需, string) 你想在指定的<code class="literal">&lt;field&gt;</code>中搜索的词。
</dd>
<dt>
<span class="term">
<code class="literal">fuzziness</code>
</span>
</dt>
<dd>
(可选, string) 匹配允许的最大编辑距离。有效值及更多信息请参考 <a class="xref" href="common-options.html#fuzziness" title="模糊性" rel="nofollow">模糊性</a>。
</dd>
<dt>
<span class="term">
<code class="literal">max_expansions</code>
</span>
</dt>
<dd>
<p>(可选, integer) 可创建的最大变形词的数量。 默认为 <code class="literal">50</code>。</p>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>
避免在 <code class="literal">max_expansions</code> 参数中使用很大的值，尤其是当 <code class="literal">prefix_length</code> 参数值为 <code class="literal">0</code> 时。

一个很大的 <code class="literal">max_expansions</code> 参数值可能会导致性能下降，因为要检查大量的变形词。
</p>
</div>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">prefix_length</code>
</span>
</dt>
<dd>
(可选, integer) 创建扩展时，开头字符保持不变的数量。默认为 <code class="literal">0</code>。
</dd>
<dt>
<span class="term">
<code class="literal">transpositions</code>
</span>
</dt>
<dd>
(可选, boolean) 指示编辑是否包括两个相邻字符的换位 (ab → ba)。默认为 <code class="literal">true</code>。
</dd>
<dt>
<span class="term">
<code class="literal">rewrite</code>
</span>
</dt>
<dd>
(可选, string) 用于重写查询的方法。 有效值及更多信息请参考 <a class="xref" href="query-dsl-multi-term-rewrite.html" title="重写参数" rel="nofollow"><code class="literal">rewrite</code> 参数</a>。
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_notes_5"></a>注意
</h3>
</div></div></div>
<p>
如果 <a class="xref" href="query-dsl.html#query-dsl-allow-expensive-queries"><code class="literal">search.allow_expensive_queries</code></a> 设置为false，将不会执行模糊查询。
</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="query-dsl-exists-query.html">« exists 查询</a>
</span>
<span class="next">
<a href="query-dsl-ids-query.html">ids »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>